package org.iteam.xlz.manager.action.sys;

import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.core.Constant.Function;
import org.iteam.core.Constant.Permission;
import org.iteam.core.Constant.Status;
import org.iteam.core.model.sys.SysRole;
import org.iteam.core.model.sys.SysUser;
import org.iteam.core.service.sys.SysRoleService;
import org.iteam.core.view.sys.PermissionView;
import org.iteam.web.struts.action.AbstractModuleAction;
import org.iteam.web.struts.action.RestAction;

import com.opensymphony.xwork2.ModelDriven;


/**
 * 角色管理
 * 
 * @author h
 *
 */
@SuppressWarnings("unchecked")
@Results({
		@Result(type = "freemarker", name = "INDEX", location = "/WEB-INF/content/sys/role-index.html"),
		@Result(type = "freemarker", name = "EDIT", location = "/WEB-INF/content/sys/role-edit.html") })
public class SysRoleAction extends AbstractModuleAction<SysRole> implements RestAction, ModelDriven<SysRole> {

	/**
	 * 列表页
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(PATH + "/index")
	public String index() {
		map.put("status", Status.values());
		return INDEX;
	}

	/**
	 * 客户信息管理列表查询
	 * 
	 * @return
	 */
	@Action(PATH + "/query")
	public String query() {
		try {
			map.put("total", service.count());
			map.put("rows", service.queryByPage(page, rows));
		} catch (Exception e) {
			e.printStackTrace();
			map.put("total", 0);
			map.put("rows", new ArrayList<SysUser>());
		}
		return MAP;
	}

	/**
	 * 效验
	 * 
	 */
	@Action(PATH + "/validate/code")
	public void validateCode() {
		try {
			print(service.validate(model.getCode()) == 0);
		} catch (SQLException e) {
			e.printStackTrace();
			print(false);
		}
	}

	/**
	 * 查看单条记录
	 * 
	 * @return
	 */
	@Action(PATH + "/{_id}")
	public String show() {
		return null;
	}

	/**
	 * 创建记录
	 * 
	 * @return
	 */
	@Action(PATH + "/create")
	public String create() {
		try {
			map.put("state", service.create(model));
		} catch (Exception e) {
			map.put("state", false);
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 更新记录
	 * 
	 * @return
	 */
	@Action(PATH + "/update")
	public String update() {
		try {
			map.put("state", service.update(model));
		} catch (SQLException e) {
			map.put("state", false);
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 获取记录
	 * 
	 * @return
	 */
	@Action(PATH + "/{_id}/sub")
	public String querySub() {
		try {
			map.put("rows", service.getGridNodes(_id));
			// service.querySub(page, rows, _id));
			map.put("total", service.countSub(_id));
		} catch (Exception e) {
			map.put("module", new ArrayList<PermissionView>());
			map.put("total", 0);
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 设置权限
	 * 
	 * @return
	 */
	@Action(PATH + "/{_id}/permissions")
	public String updateRoleFunction() {
		try {
			map.put("state", service.updateRoleFunctions(_id, _module, _permission, _type));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 删除记录
	 * 
	 * @return
	 */
	@Action(PATH + "/{_id}/delete")
	public String delete() {
		try {
			map.put("state", service.delete(_id));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 销毁记录
	 * 
	 * @return
	 */
	@Action(PATH + "/{_id}/destroy")
	public String destroy() {
		try {
			map.put("state", service.destroy(_id));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 编辑记录
	 * 
	 * @return
	 */

	@Action(PATH + "/{_id}/edit")
	public String edit() {
		try {
			object = service.show(_id);
			map.put("mode", Function.Mode.values());
			map.put("_mode", Permission.Mode.values());
			map.put("status", Status.values());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return EDIT;
	}

	/**
	 * 新增记录
	 * 
	 * @return
	 */
	@Action(PATH + "/new")
	public String editNew()  {
		object = model;
		map.put("status", Status.values());
		map.put("mode", Function.Mode.values());
		map.put("_mode", Permission.Mode.values());
		return EDIT;
	}

	public SysRole getModel() {
		if (model == null)
			model = new SysRole();
		return model;
	}

	private static final long serialVersionUID = -5800011834083416221L;
	private SysRoleService service = SysRoleService.getInstance();
	private boolean _permission;
	private String _module;
	private int _type = -1;
	private final String PATH = "/sys/role";

	public boolean is_permission() {
		return _permission;
	}

	public void set_permission(boolean _permission) {
		this._permission = _permission;
	}

	public String get_module() {
		return _module;
	}

	public void set_module(String _module) {
		this._module = _module;
	}

	public int get_type() {
		return _type;
	}

	public void set_type(int _type) {
		this._type = _type;
	}
}
